home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / python-gnome2 / examples / bonobo / unknown.py < prev   
Encoding:
Python Source  |  2009-03-14  |  2.0 KB  |  73 lines

  1. #! /usr/bin/env python
  2. import pygtk; pygtk.require("2.0")
  3.  
  4. import bonobo
  5. import Bonobo
  6. import Bonobo__POA
  7. import CORBA, sys
  8. import gc
  9.  
  10. gc.set_debug(gc.DEBUG_LEAK)
  11.  
  12. orb = CORBA.ORB_init(sys.argv)
  13. bonobo.activate()
  14.  
  15. class PersistStream(Bonobo__POA.PersistStream, bonobo.UnknownBaseImpl):
  16.     def __init__(self):
  17.         bonobo.UnknownBaseImpl.__init__(self)
  18.  
  19.     # implementations of methods/attributes of "Interface" go here
  20.  
  21.     def load(self, content_type):
  22.     print "PersistStream::load(content_type='%s')" % content_type
  23.  
  24.     def save(self, content_type):
  25.     print "PersistStream::save(content_type='%s')" % content_type
  26.  
  27.  
  28. ps = PersistStream()
  29.  
  30. def foo(*args):
  31.     print args
  32.  
  33. listener = bonobo.Listener(foo)
  34. listener.add_interface(ps.get_bonobo_object())
  35.  
  36. del ps
  37. ## How is it that the PersistStream servant instance is kept alive, I
  38. ## hear you ask?
  39. ##     Well, there's a signal connection from the 'destroy' signal of
  40. ## the ForeignObject to a bound method of the servant instance.  A
  41. ## bound method object contains an implicit reference to the instance
  42. ## to which it is bound.  On the other hand, the servant contains a
  43. ## reference to the ForeignObject.  This way, these two objects keep
  44. ## each other alive with mutual references.
  45. ##     When the 'destroy' signal is fired (when bonobo reference count
  46. ## drops to zero), the bound method in the servant is called, and it
  47. ## deletes the reference it has to the ForeignObject, thus destroying
  48. ## the signal connection (closure), consequently releasing the last
  49. ## reference to the servant.  At least that's my theory on how things
  50. ## work..
  51.  
  52.  
  53. print "server-side boundary"
  54. listener = listener.corba_objref() # from now on we work on client-side
  55. gc.collect()
  56. print "client-side"
  57.  
  58. print "query PersistStream"
  59. ps = listener.queryInterface("IDL:Bonobo/PersistStream:1.0")
  60. print ps
  61. print "unref PersistStream"
  62. if ps is not None:
  63.     ps.unref()
  64. gc.collect()
  65.  
  66. print ">>unref listener start"
  67. # this should trigger final ref count -> destroy
  68. listener.unref()
  69. del listener
  70. print "<<unref listener end"
  71. gc.collect()
  72.  
  73.